const mongoose = require("mongoose");

//登录
const login = async ({ username, password }) => {
    const usersModel = mongoose.model("users");
    const data = await usersModel.findOne({ username, password });
    let status = true;
    if (data) {
        return { status, data };
    } else {
        status = false;
        return status;
    }
};

//注册
const reg = async ({ username, password }) => {
    const usersModel = mongoose.model("users");
    const data = await usersModel.create({ username: username, password })
    const admin = await usersModel.findOne({ _id: data._id });

    let status = true;
    if (admin) {
        return status;
    } else {
        status = false;
        return status;
    }

};

//删除订单
const delOrder = async (_id, index) => {
    // 获取用户模型
    const usersModel = mongoose.model("users");
    // 找到对应用户
    let users = await usersModel.findOne({ _id });
    // 获取当前订单条数
    const oldOrder = users.order.length;
    // 删除需要对应下标的内容
    users.order.splice(index, 1);
    // 更新用户订单数据
    await usersModel.updateOne({ _id }, { order: users.order });
    // 查找用户最新数据
    users = await usersModel.findOne({ _id });
    let status = true;
    if (users.order.length !== oldOrder) {
        return status;
    } else {
        status = false;
        return status;
    }
}

// 添加订单
const addOrder = async ({ str, scheduleId, tatol, _id }) => {
    const usersModel = mongoose.model("users");
    const users = await usersModel.findOne({ _id });
    users.order.push([{ str, scheduleId, tatol, _id }])
    console.log(users.order);
    // 添加文档到模型
    await usersModel.updateOne({ _id }, { order: users.order });
    const data = { status: true };
    return data;
}

// 获取用户
const getOne = async _id => {
    const usersModel = mongoose.model("users");
    // 添加文档到模型
    return  await usersModel.find({ name: _id });
}
// 查询
const getsearch = async username => {
    const usersModel = mongoose.model("users");
    // find的方法查询条件必须是个对象  new RegExp(username)模糊查询
    const data = await usersModel.find({ username: new RegExp(username) })
    return data
}

// 用户列表

const get = async ({ curPage, eachPage }) => {
    (curPage = ~~curPage), (eachPage = ~~eachPage);
    const data = {
        total: 0,
        maxPage: 0,
        rows: []
    }
    //model只有一个参数代表获取
    const usersModel = mongoose.model("users");
    //获取当前model映射的那个集合里面存在的文档总数
    data.total = await usersModel.countDocuments();
    data.maxPage = Math.ceil(data.total / eachPage);
    const skipNum = (curPage - 1) * eachPage;
    //exec执行populate填充
    data.rows = await usersModel.find().populate("grade").skip(skipNum).limit(eachPage).sort({ _id: -1 }).exec();
    return data;

}
const Delete = async _id => {
    const usersModel = mongoose.model("users");
    await usersModel.remove({ _id })
    const data = { status: true };
    return data
}


// 获取用户
const getone = async _id => {
    const usersModel = mongoose.model("users");
    // 添加文档到模型
    return  await usersModel.find({_id });
}












module.exports = { login, reg, addOrder, getOne, delOrder, get, Delete, getsearch ,getone}